package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcAuthTemplateService;
import com.soft.idc.common.service.IdcMenuService;
import com.soft.idc.common.util.JQPage;

public class MenuConfigAuthController extends Controller {
	public void index() {
		this.render("/menu_config_auth/menu_config_auth.html");
	}

	public void query() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String menu_name = this.getPara("menu_name");
		String auth_name = this.getPara("auth_name");
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT R.REL_ID, R.MENU_ID, R.AUTH_ID, M.NAME AS MENU_NAME, ");
		sb.append("T.NAME AS AUTH_TEMPLATE_NAME ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append(" FROM IDC_AUTH_MENU_REL R LEFT JOIN IDC_MENU M ON R.MENU_ID = M.MENU_ID ");
		sqlFrom.append(" LEFT JOIN IDC_AUTH_TEMPLATE T ON R.AUTH_ID = T.AUTH_ID WHERE 1=1 ");
		if(menu_name !=null ){
			sqlFrom.append(" AND M.NAME LIKE '%" + menu_name + "%' ");
		}
		
		if(auth_name !=null ){
			sqlFrom.append(" and T.NAME like '%" + auth_name + "%' ");
		}
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		this.renderJson(jPage);
	}

	public void add() {
		String menu_id = this.getPara("menu_id");
		String auth_template_id = this.getPara("auth_template_id");
		String sql = "INSERT INTO IDC_AUTH_MENU_REL (MENU_ID, AUTH_ID) VALUES (? , ? )";
		int  result = Db.update(sql, new Object[]{menu_id, auth_template_id});
		this.renderJson(result);
	}

	public void delete() {
		final String relIds = this.getPara("relIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] roleIdArr = relIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_AUTH_MENU_REL WHERE REL_ID IN(");
				for(int i=0; i<roleIdArr.length; i++){
					if(i==roleIdArr.length-1){
						sb.append(roleIdArr[i] + ")");
					}else{
						sb.append(roleIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==roleIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String relId = this.getPara("relId");
		String menuId = this.getPara("menuId");
		String authTemplateId = this.getPara("authTemplateId");
		String sql = "UPDATE IDC_AUTH_MENU_REL SET MENU_ID=?, AUTH_ID=? WHERE REL_ID =? ";
		int  result = Db.update(sql, new Object[]{menuId, authTemplateId, relId});
		this.renderJson(result);
	}
	
	public void queryAuthTemplate(){
		List<Record> result = IdcAuthTemplateService.loadAuthTemplate();
		this.renderJson(result);
	}
}
